package com.xzx.study;

import android.content.DialogInterface;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.telephony.SmsManager;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import com.xzx.study.animation.AnimationApp1Activity;
import com.xzx.study.animation.AnimationApp2Activity;
import com.xzx.study.animation.DrawableAnimationActivity;
import com.xzx.study.animation.ViewAnimationActivity;
import com.xzx.study.broadcast.BroadcastActivity;
import com.xzx.study.drawable.DrawableActivity;
import com.xzx.study.fragment.FragmentDemoActivity;
import com.xzx.study.provider.ContentProviderActivity;
import com.xzx.study.service.ServiceActivity;
import com.yanzhenjie.permission.Action;
import com.yanzhenjie.permission.AndPermission;
import com.yanzhenjie.permission.runtime.Permission;

import java.util.List;

public class MainActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {

    private static final String TAG = "MainActivity";

    private EditText et_main_msg;
    private Button btn_main_start1;
    private Button btn_main_start2;

    private EditText et_main_phone;
    private EditText et_main_content;
    private Button btn_main_call;
    private Button btn_main_send;

    private Button btn_main_simple_component;
    private Button btn_main_menu;
    private Button btn_main_progress;
    private Button btn_main_dialog;

    private Button btn_main_linearlayout;
    private Button btn_main_relativelayout;
    private Button btn_main_fremalayout;

    private Button btn_main_listview_arrayadapter;
    private Button btn_main_listview_simpleadapter;
    private Button btn_main_listview_baseadapter;

    private Button btn_main_shared_preferences;
    private Button btn_main_inner_file;
    private Button btn_main_out_file;
    private Button btn_main_sqlite;

    private Button btn_main_black_list_demo;
    private Button btn_main_network;

    private Button btn_main_handler;
    private Button btn_main_imageLoader;

    private Button btn_main_fragment;
    private Button btn_main_event;
    private Button btn_main_service;
    private Button btn_main_broadcast;
    private Button btn_main_provider;
    private Button btn_main_view_animation;
    private Button btn_main_drawable_animation;

    private Button btn_main_animation_app1;
    private Button btn_main_animation_app2;

    private Button btn_main_drawable;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        btn_main_drawable = findViewById(R.id.btn_main_drawable);
        btn_main_drawable.setOnClickListener(this);

        btn_main_animation_app1 = findViewById(R.id.btn_main_animation_app1);
        btn_main_animation_app2 = findViewById(R.id.btn_main_animation_app2);

        btn_main_animation_app1.setOnClickListener(this);
        btn_main_animation_app2.setOnClickListener(this);

        btn_main_drawable_animation = findViewById(R.id.btn_main_drawable_animation);
        btn_main_drawable_animation.setOnClickListener(this);

        btn_main_view_animation = findViewById(R.id.btn_main_view_animation);
        btn_main_view_animation.setOnClickListener(this);

        btn_main_provider = findViewById(R.id.btn_main_provider);
        btn_main_provider.setOnClickListener(this);

        btn_main_broadcast = findViewById(R.id.btn_main_broadcast);
        btn_main_broadcast.setOnClickListener(this);

        btn_main_service = findViewById(R.id.btn_main_service);
        btn_main_service.setOnClickListener(this);
        btn_main_event = findViewById(R.id.btn_main_event);
        btn_main_event.setOnClickListener(this);

        btn_main_fragment = findViewById(R.id.btn_main_fragment);
        btn_main_fragment.setOnClickListener(this);

        btn_main_imageLoader = findViewById(R.id.btn_main_imageLoader);
        btn_main_imageLoader.setOnClickListener(this);

        btn_main_handler = findViewById(R.id.btn_main_handler);
        btn_main_handler.setOnClickListener(this);

        btn_main_network = findViewById(R.id.btn_main_network);
        btn_main_network.setOnClickListener(this);

        btn_main_black_list_demo = findViewById(R.id.btn_main_black_list_demo);
        btn_main_black_list_demo.setOnClickListener(this);

        btn_main_sqlite = findViewById(R.id.btn_main_sqlite);
        btn_main_sqlite.setOnClickListener(this);

        btn_main_out_file = findViewById(R.id.btn_main_out_file);
        btn_main_out_file.setOnClickListener(this);

        btn_main_inner_file = findViewById(R.id.btn_main_inner_file);
        btn_main_inner_file.setOnClickListener(this);

        btn_main_shared_preferences = findViewById(R.id.btn_main_shared_preferences);
        btn_main_shared_preferences.setOnClickListener(this);

        et_main_msg = findViewById(R.id.et_main_msg);
        btn_main_start1 = findViewById(R.id.btn_main_start1);
        btn_main_start2 = findViewById(R.id.btn_main_start2);

        btn_main_start1.setOnClickListener(this);
        btn_main_start2.setOnClickListener(this);

        et_main_phone = findViewById(R.id.et_main_phone);
        et_main_content = findViewById(R.id.et_main_content);
        btn_main_call = findViewById(R.id.btn_main_call);
        btn_main_send = findViewById(R.id.btn_main_send);

        btn_main_call.setOnClickListener(this);
        btn_main_send.setOnClickListener(this);
        btn_main_call.setOnLongClickListener(this);
        btn_main_send.setOnLongClickListener(this);


        if (AndPermission.hasAlwaysDeniedPermission(this, Permission.CALL_PHONE, Permission.SEND_SMS)) {
            AndPermission.with(this).runtime().permission(Permission.CALL_PHONE, Permission.SEND_SMS).start();
        }

        btn_main_simple_component = findViewById(R.id.btn_main_simple_component);
        btn_main_simple_component.setOnClickListener(this);

        btn_main_menu = findViewById(R.id.btn_main_menu);
        btn_main_menu.setOnClickListener(this);

        btn_main_progress = findViewById(R.id.btn_main_progress);
        btn_main_progress.setOnClickListener(this);

        btn_main_dialog = findViewById(R.id.btn_main_dialog);
        btn_main_dialog.setOnClickListener(this);

        btn_main_linearlayout = findViewById(R.id.btn_main_linearlayout);
        btn_main_linearlayout.setOnClickListener(this);
        btn_main_relativelayout = findViewById(R.id.btn_main_relativelayout);
        btn_main_relativelayout.setOnClickListener(this);
        btn_main_fremalayout = findViewById(R.id.btn_main_fremalayout);
        btn_main_fremalayout.setOnClickListener(this);

        btn_main_listview_arrayadapter = findViewById(R.id.btn_main_listview_arrayadapter);
        btn_main_listview_simpleadapter = findViewById(R.id.btn_main_listview_simpleadapter);
        btn_main_listview_baseadapter = findViewById(R.id.btn_main_listview_baseadapter);
        btn_main_listview_arrayadapter.setOnClickListener(this);
        btn_main_listview_simpleadapter.setOnClickListener(this);
        btn_main_listview_baseadapter.setOnClickListener(this);

    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        if (btn_main_drawable == v) {
            startActivity(new Intent(this, DrawableActivity.class));
            return;
        }

        if (btn_main_animation_app1 == v) {
            startActivity(new Intent(this, AnimationApp1Activity.class));
            return;
        }

        if (btn_main_animation_app2 == v) {
            startActivity(new Intent(this, AnimationApp2Activity.class));
            return;
        }

        if (btn_main_drawable_animation == v) {
            startActivity(new Intent(this, DrawableAnimationActivity.class));
            return;
        }

        if (btn_main_view_animation == v) {
            startActivity(new Intent(this, ViewAnimationActivity.class));
            return;
        }
        if (btn_main_provider == v) {
            startActivity(new Intent(this, ContentProviderActivity.class));
            return;
        }
        if (btn_main_broadcast == v) {
            startActivity(new Intent(this, BroadcastActivity.class));
            return;
        }
        if (btn_main_service == v) {
            startActivity(new Intent(this, ServiceActivity.class));
            return;
        }
        if (btn_main_event == v) {
            startActivity(new Intent(this, MotionActivity.class));
            return;
        }
        if (btn_main_fragment == v) {
            startActivity(new Intent(this, FragmentDemoActivity.class));
            return;
        }
        if (btn_main_imageLoader == v) {
            startActivity(new Intent(this, ImageLoaderActivity.class));
            return;
        }
        if (v == btn_main_handler) {
            startActivity(new Intent(this, MessageHandlerActivity.class));
            return;
        }
        if (btn_main_network == v) {
            startActivity(new Intent(this, NetworkActivity.class));
            return;
        }
        if (v == btn_main_black_list_demo) {
            startActivity(new Intent(this, BlackListActivity.class));
            return;
        }
        if (v == btn_main_sqlite) {
            startActivity(new Intent(this, SQLiteActivity.class));
            return;
        }
        if (v == btn_main_out_file) {
            startActivity(new Intent(this, OutFileActivity.class));
            return;
        }
        if (v == btn_main_inner_file) {
            startActivity(new Intent(this, InnerFileActivity.class));
            return;
        }
        if (v == btn_main_shared_preferences) {
            startActivity(new Intent(this, SharedPreferenceActivity.class));
        } else if (v == btn_main_start1) {
            Toast.makeText(this, "一般回调", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this, SecondActivity.class);
            intent.putExtra("msg", et_main_msg.getText().toString());
            startActivity(intent);
        } else if (v == btn_main_start2) {
            Toast.makeText(this, " 带回调返回", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(this, SecondActivity.class);
            intent.putExtra("msg", et_main_msg.getText().toString());
            int requestCode = 1;
            startActivityForResult(intent, requestCode);
        } else if (v == btn_main_call) {
            Toast.makeText(this, "去拨打电话", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Intent.ACTION_DIAL);//
            intent.setData(Uri.parse("tel:" + et_main_phone.getText().toString()));
            startActivity(intent);
        } else if (v == btn_main_send) {
            Toast.makeText(this, "去发送短信", Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(Intent.ACTION_SENDTO);//
            intent.setData(Uri.parse("smsto:" + et_main_phone.getText().toString()));
            intent.putExtra("sms_body", et_main_content.getText().toString());
            startActivity(intent);
        } else if (v == btn_main_simple_component) {
            startActivity(new Intent(this, SimpleComponentActivity.class));
        } else if (v == btn_main_menu) {
            startActivity(new Intent(this, MenuActivity.class));
        } else if (v == btn_main_progress) {
            startActivity(new Intent(this, ProgressActivity.class));
        } else if (v == btn_main_dialog) {
            startActivity(new Intent(this, DialogActivity.class));
        } else if (v == btn_main_linearlayout) {
            startActivity(new Intent(this, LinearLayoutActivity.class));
        } else if (v == btn_main_relativelayout) {
            startActivity(new Intent(this, RelativeLayoutActivity.class));
        } else if (v == btn_main_fremalayout) {
            startActivity(new Intent(this, FrameActivity.class));
        } else if (v == btn_main_listview_arrayadapter) {
            startActivity(new Intent(this, ListViewArrayAdapterActivity.class));
        } else if (v == btn_main_listview_simpleadapter) {
            startActivity(new Intent(this, ListViewSimpleAdapterActivity.class));
        } else if (v == btn_main_listview_baseadapter) {
            startActivity(new Intent(this, ListViewBaseAdapterActivity.class));
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == 1) {// btn_main_start2这个按钮触发的
            if (resultCode == 2) {// SecondActivity返回的
                String result = data.getStringExtra("result");
                et_main_msg.setText(result);
            }
        }
    }

    @Override
    public boolean onLongClick(View v) {
        if (v == btn_main_call) {
            //            Toast.makeText(this, "长按拨打电话", Toast.LENGTH_SHORT).show();
            Log.i(TAG, "长按拨打电话");
            if (AndPermission.hasPermissions(this, Permission.CALL_PHONE)) {
                Toast.makeText(MainActivity.this, "有权限了", Toast.LENGTH_SHORT).show();
            } else {
                Toast.makeText(MainActivity.this, "还没有权限", Toast.LENGTH_SHORT).show();
                AndPermission.with(this).runtime().permission(Permission.CALL_PHONE).onGranted(
                        new Action<List<String>>() {
                            @Override
                            public void onAction(List<String> data) {
                                Toast.makeText(MainActivity.this, "授权了", Toast.LENGTH_SHORT).show();
                                Intent intent = new Intent(Intent.ACTION_CALL);//
                                intent.setData(Uri.parse("tel:" + et_main_phone.getText().toString()));
                                startActivity(intent);
                            }
                        }).onDenied(new Action<List<String>>() {
                    @Override
                    public void onAction(List<String> data) {
                        Toast.makeText(MainActivity.this, "拒绝了", Toast.LENGTH_SHORT).show();
                        new AlertDialog.Builder(MainActivity.this).setTitle("授权提示").setMessage("此权限是必须的").setNegativeButton(
                                "去设置", new DialogInterface.OnClickListener() {
                                    @Override
                                    public void onClick(DialogInterface dialog, int which) {
                                        Toast.makeText(MainActivity.this, "去设置", Toast.LENGTH_SHORT).show();
                                    }
                                }).create().show();
                    }
                }).start();
            }

        } else if (v == btn_main_send) {
            Toast.makeText(this, "长按发送短信", Toast.LENGTH_SHORT).show();
            SmsManager smsManager = SmsManager.getDefault();
            smsManager.sendTextMessage(et_main_phone.getText().toString(), null, et_main_content.getText().toString(),
                                       null, null);
        }
        return true; // 表示此事件已经消费
    }

    private boolean needBack = false;

    // 2秒退出
    @Override
    public boolean onKeyUp(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {

            if (needBack) {
                finish();
            } else {
                new Handler().postDelayed(() -> {
                    needBack = false;
                }, 2000);
            }
            Toast.makeText(this, "连按2次退出", Toast.LENGTH_SHORT).show();
            needBack = true;
        }
        return true;
    }
}
